require(["config"], function () {
    require(["jquery", "jquery.cookie"], function () {
        $(function () {
            $(".top").load("shareHeader.html");
            $(".foot").load("shareFooter.html");
            //购物车
            //拿取Cookie
            if (sessionStorage.getItem("ifLogin")) {
                var objCookie = JSON.parse(sessionStorage.getItem("ifLogin") || "{}");
                var username = objCookie.uname;
                console.log(username);
                $.ajax({
                    url: "../../server/shoppingCart.php",
                    data: {"usname": username}
                }).then(function (res) {
                    console.log(res);
                    if (res.status == 1) {
                        var resArr = res.data;  //数组
                        for (var i = 0; i < resArr.length; i++) {
                            var str = "";
                            str += `<tr><td><input type="checkbox"></td>`;
                            str += `<td><div><a style="vertical-align: middle"><img src="${resArr[i]
                                .p_img}"></a><a href="#" style="vertical-align: middle">${resArr[i].p_title}</a></div></td>`;
                            str += `<td>${resArr[i].p_id}</td>`;
                            str += `<td>${resArr[i].p_price}</td>`;
                            str += `<td>有库存</td>`;
                            str += `<td>${resArr[i].p_num}</td>`;
                            str += `<td>${resArr[i].p_total}</td>`;
                            str += `<td>删除</td>`;
                            $("table").append(str)
                        }
                        $(".goods_order>div>em").on("click",function () {
                            alert("订单已经提交！！！")
                        })
                    }
                })
            } else {
                //没有登录去Cookie拿取数据
                var arrCookie = JSON.parse($.cookie("cart"));
                console.log(arrCookie);
                for (var i = 0; i < arrCookie.length; i++) {
                    arrCookie[i].total = arrCookie[i].num * arrCookie[i].price;
                    console.log(arrCookie[i]);
                    var str = "";
                    str += `<tr><td><input type="checkbox"></td>`;
                    str += `<td><div style="vertical-align: middle"><a style="vertical-align: middle"><img src="${arrCookie[i]
                        .mainImg}"></a><a href="#" style="vertical-align: middle">${arrCookie[i].title}</a></a></div></td>`;
                    str += `<td>${arrCookie[i].pid}</td>`;
                    str += `<td>${arrCookie[i].price}</td>`;
                    str += `<td>有库存</td>`;
                    str += `<td>${arrCookie[i].num}</td>`;
                    str += `<td>${arrCookie[i].total}</td>`;
                    str += `<td>删除</td>`;
                    $("table").append(str);
                }
                $(".goods_order>div>em").on("click",function () {
                    console.log(111);
                    window.location.assign("login.html");
                })
            }
            $(".goods_order>div>a").on("click",function () {
                window.location.assign("homePage.html");
            })
            //操作checkbox
            console.log(console.log($(":checkbox")));
        })
    })
})